﻿/*
==================================================================
    注意：此文件为Auto代码，不可手动修改【权限】
==================================================================
*/

namespace Services
{
    /// <summary>
    /// Auto权限
    /// </summary>
    public abstract class AutoPermissionMapping : BaseMapping<Permission, PermissionModel, PermissionFilterModel>
    {
        public AutoPermissionMapping(ICurrentUser currentUser)
            : base(currentUser) { }

        #region 读数据
        /// <summary>
        /// 读数据，从Entity对象转成Model对象
        /// </summary>
        /// <param name="entity">Entity对象</param>
        /// <returns></returns>
        public override PermissionModel Entity2Model(Permission entity)
        {
            PermissionModel model = new PermissionModel();

            model.PermissionID = entity.PermissionID;
            model.PerID = entity.PerID;
            model.Name = entity.Name;
            model.ParentID = entity.ParentID;
            model.GroupNum = entity.GroupNum;
            model.IsUseRegulator = entity.IsUseRegulator;
            model.SortNo = entity.SortNo;

            return model;
        }
        #endregion

        #region 写数据
        /// <summary>
        /// 写数据，从Model对象转换成Entity对象
        /// </summary>
        /// <param name="model">ViewModel对象</param>
        /// <returns></returns>
        public override Permission Model2Entity(PermissionModel model)
        {
            Permission entity = new Permission();

            entity.PermissionID = model.PermissionID;
            entity.PerID = model.PerID;
            entity.Name = model.Name;
            entity.ParentID = model.ParentID;
            entity.GroupNum = model.GroupNum;
            entity.IsUseRegulator = model.IsUseRegulator;
            entity.SortNo = model.SortNo;

            return entity;
        }
        #endregion

        #region 搜索条件转换
        /// <summary>
        /// 搜索条件转换，从FilterModel对象转换成Where对象
        /// </summary>
        /// <param name="filterModel">FilterModel对象</param>
        /// <returns></returns>
        public override Expressionable<Permission> FilterModel2Where(PermissionFilterModel filterModel)
        {
            var where = base.FilterModel2Where(filterModel);

            //编号
            if (filterModel._PermissionID.HasValue)
            {
                where = where.And(x => x.PermissionID == filterModel._PermissionID.Value);
            }
            if (filterModel._PermissionIDs.IsNotEmpty() && filterModel._PermissionIDs.Count > 0)
            {
                where = where.And(x => filterModel._PermissionIDs.Contains(x.PermissionID));
            }
            if (filterModel._NotPermissionIDs.IsNotEmpty() && filterModel._NotPermissionIDs.Count > 0)
            {
                where = where.And(x => !filterModel._NotPermissionIDs.Contains(x.PermissionID));
            }

            //名称
            if (!string.IsNullOrEmpty(filterModel._Name))
            {
                where = where.And(x => x.Name.Contains(filterModel._Name));
            }
            if (!string.IsNullOrEmpty(filterModel._NameEqual))
            {
                where = where.And(x => x.Name == filterModel._NameEqual);
            }

            //是否为监管部门用
            if (filterModel._IsUseRegulator.HasValue)
            {
                where = where.And(x => x.IsUseRegulator == filterModel._IsUseRegulator.Value);
            }
            if (filterModel._IsUseRegulators.IsNotEmpty() && filterModel._IsUseRegulators.Count > 0)
            {
                where = where.And(x => filterModel._IsUseRegulators.Contains(x.IsUseRegulator));
            }
            
            return where;
        }
        #endregion
    }
}

